package com.amazon.inapp.purchasing;

import android.os.RemoteException;
import com.amazon.android.framework.exception.KiwiException;
import com.amazon.venezia.command.SuccessResult;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import org.json.JSONException;
import org.json.JSONObject;

final class KiwiGetItemDataRequestCommandTask extends KiwiBaseCommandTask
{
  private static final String COMMAND_NAME = "getItem_data";
  private static final String COMMAND_VERSION = "1.0";
  private static final String TAG = "KiwiGetItemDataRequestCommandTask";
  private final Set<String> _skus;

  KiwiGetItemDataRequestCommandTask(Set<String> paramSet, String paramString)
  {
    super("getItem_data", "1.0", paramString);
    this._skus = paramSet;
    addCommandData("skus", this._skus);
  }

  private ItemDataResponse getFailedResponse()
  {
    return new ItemDataResponse(getRequestId(), null, ItemDataResponse.ItemDataRequestStatus.FAILED, null);
  }

  private void notifyObserver(final ItemDataResponse paramItemDataResponse)
  {
    postRunnableToMainLooper(new Runnable()
    {
      public void run()
      {
        PurchasingObserver localPurchasingObserver = PurchasingManager.getPurchasingObserver();
        if (localPurchasingObserver != null)
        {
          if (Logger.isTraceOn())
            Logger.trace("KiwiGetItemDataRequestCommandTask", "Invoking onItemDataResponse with " + paramItemDataResponse);
          localPurchasingObserver.onItemDataResponse(paramItemDataResponse);
        }
      }
    });
  }

  protected void onSuccess(SuccessResult paramSuccessResult)
    throws RemoteException, KiwiException
  {
    if (Logger.isTraceOn())
      Logger.trace("KiwiGetItemDataRequestCommandTask", "onSuccess");
    Map localMap = paramSuccessResult.getData();
    if (Logger.isTraceOn())
      Logger.trace("KiwiGetItemDataRequestCommandTask", "data: " + localMap);
    String str1 = (String)localMap.get("errorMessage");
    if (Logger.isTraceOn())
      Logger.trace("KiwiGetItemDataRequestCommandTask", "onSuccess: errorMessage: \"" + str1 + "\"");
    if (isNullOrEmpty(str1))
    {
      HashSet localHashSet = new HashSet();
      HashMap localHashMap = new HashMap();
      Iterator localIterator = this._skus.iterator();
      while (localIterator.hasNext())
      {
        String str2 = (String)localIterator.next();
        if (!localMap.containsKey(str2))
        {
          localHashSet.add(str2);
        }
        else
        {
          try
          {
            JSONObject localJSONObject = new JSONObject((String)localMap.get(str2));
            String str3 = localJSONObject.getString("title");
            String str4 = localJSONObject.getString("description");
            String str5 = localJSONObject.getString("iconUrl");
            localHashMap.put(str2, new Item(str2, localJSONObject.optString("price"), Item.ItemType.valueOf(localJSONObject.getString("itemType")), str3, str4, str5));
          }
          catch (JSONException localJSONException)
          {
            localHashSet.add(str2);
          }
          if (Logger.isErrorOn())
            Logger.error("KiwiGetItemDataRequestCommandTask", "Error parsing JSON for SKU " + str2 + ": " + localJSONException.getMessage());
        }
      }
      if (localHashSet.isEmpty());
      for (ItemDataResponse.ItemDataRequestStatus localItemDataRequestStatus = ItemDataResponse.ItemDataRequestStatus.SUCCESSFUL; ; localItemDataRequestStatus = ItemDataResponse.ItemDataRequestStatus.SUCCESSFUL_WITH_UNAVAILABLE_SKUS)
      {
        notifyObserver(new ItemDataResponse(getRequestId(), localHashSet, localItemDataRequestStatus, localHashMap));
        return;
      }
    }
    if (Logger.isTraceOn())
      Logger.trace("KiwiGetItemDataRequestCommandTask", "found error message: " + str1);
    sendFailedResponse();
  }

  protected void sendFailedResponse()
  {
    notifyObserver(getFailedResponse());
  }
}

/* Location:           E:\android\Toy Defense 2_dex2jar.jar
 * Qualified Name:     com.amazon.inapp.purchasing.KiwiGetItemDataRequestCommandTask
 * JD-Core Version:    0.6.2
 */